﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace TestEngine
{
    class Camera
    {
        public Vector3 position; //Make Translate at home!
        public Vector3 forward;
        public Vector3 up;
        public Vector3 left;

        public Matrix Proj;
        public Matrix View;

        public Actor3D targetToFollow;

        public bool isThirdPerson = false;

        public Camera()
        {
            position = new Vector3(0, 0, -100.0f);//Vector3.Zero;
            forward = Vector3.UnitZ;
            up = Vector3.UnitY;
            left = -Vector3.UnitX;
            Proj = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), 800.0f / 600.0f, 1.0f, 10000.0f);
            View = Matrix.CreateLookAt(position, position + forward, up);
        }

        public Camera(Vector3 pos, Vector3 target)
        {
            position = pos;
            forward = Vector3.UnitZ; //Should be target minus pos, normalized;
            up = Vector3.UnitY;
            left = -Vector3.UnitX;
            Proj = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), 600.0f / 800.0f, 1.0f, 10000.0f);
            View = Matrix.CreateLookAt(position, target, up);
        }

        public void update(int d_time)
        {
            
            Vector3 expectedPos;
            if (isThirdPerson)
            {
                expectedPos = targetToFollow.position 
                                        - targetToFollow.forward 
                                        * 10 + targetToFollow.up;
            }
            else
            {
                expectedPos = targetToFollow.position
                                        ;
            }
                Vector3 direction = expectedPos - position;
                direction.Normalize();
                position += direction;
            View = Matrix.CreateLookAt(expectedPos, direction, up);


        }

        public void RotateCam (float rotX, float rotY, float rotZ)
        {
            forward = Vector3.Transform(forward, Matrix.CreateFromYawPitchRoll(rotY, rotX, rotZ));
            left = Vector3.Transform(left, Matrix.CreateFromYawPitchRoll(rotY, rotX, rotZ));
            up = Vector3.Transform(up, Matrix.CreateFromYawPitchRoll(rotY, rotX, rotZ));
        }

        public void RotateCam(Vector3 rotation)
        {
            forward = Vector3.Transform(forward, Matrix.CreateFromYawPitchRoll(rotation.Y, rotation.X, rotation.Z));
            left = Vector3.Transform(left, Matrix.CreateFromYawPitchRoll(rotation.Y, rotation.X, rotation.Z));
            up = Vector3.Transform(up, Matrix.CreateFromYawPitchRoll(rotation.Y, rotation.X, rotation.Z));
        }
    }
}
